/**
 * 
 */
package com.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.cms.model.Channel;
import com.cms.model.ChannelTerraceInfo;
import com.cms.model.TerraceInfo;
import com.model.Admin;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**
 * @author jun.huang
 *
 */
public class ChannelServiceImpl extends AbstractDateService implements ChannelService
{
	private Logger logger;
	
	public ChannelServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
	}
		
	@Override
	public Channel getChannel(int id) 
	{
		String sql = "select * from t_channel where id=" + String.valueOf(id) + "";
		ModelGenerator<Channel> dbcpmg = new ModelGenerator<Channel>(Channel.class);
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<Channel> getChannels()
	{
		String sql = "select * from t_channel where isdel='0' order by id desc";
		logger.debug("sql: " + sql);
		ModelGenerator<Channel> dbcpmg = new ModelGenerator<Channel>(Channel.class);
		return dbcpmg.generateList(base, sql);
	}
	

	@Override
	public List<Channel> getChannelsByAdmin(Admin admin) {
		String sql="select * from t_channel where id in (select channelid from t_adminchannel where adminid="+admin.getId()+")";
		ModelGenerator<Channel> dbcpmg = new ModelGenerator<Channel>(Channel.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean deleteChannel(Channel channel) {
		String[] sql = {"delete from t_channel where id=" + String.valueOf(channel.getId()) + "",
				"delete from t_channelterrace where channelid=" + String.valueOf(channel.getId()) + ""
		};
		return ConnectionManager.getInstance().executeBatch(base, sql);
	}
	
	@Override
	public boolean deleteChannel1(Channel channel) {
		String[] sql = {"update t_channel set isdel='1' where id=" + String.valueOf(channel.getId()) + ""};
		return ConnectionManager.getInstance().executeBatch(base, sql);
	}

	@Override
	public boolean insertChannel(Channel channel) {
		channel.setId(getTableId("t_channel"));
		String sql = 
			"insert into t_channel(id,name,company,bd,contractdate,contractend,remark,cooperationtype) " +
			"values(" + String.valueOf(channel.getId()) + ",'" + channel.getName() + "','" + channel.getCompany() + "','" +
					channel.getBd() + "','" + channel.getContractDate() + "','" + 
					channel.getContractEnd()+ "','"+channel.getRemark()+"','"+channel.getCooperationtype()+"')";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean updateChannel(Channel channel) {
		String sql = 
			"update t_channel set name='" + channel.getName() + "'," +
					"company='" + channel.getCompany() + "'," +
					"bd='" + channel.getBd() + "'," +
					"contractdate='" + channel.getContractDate() + "'," + 
					"contractend='" + channel.getContractEnd() + "'," +
					"cooperationtype='" + channel.getCooperationtype() + "'," +
					"remark='"+channel.getRemark()+"' " +
					" where id=" + String.valueOf(channel.getId()) + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}
	@Override
	public List<Channel> getChannels(String txt_keyword)
	{
		String sql = "select * from t_channel where 1=1 and isdel='0' ";
		if(txt_keyword!=null && txt_keyword.length()>0)
		{
			sql+=" and name like '%"+txt_keyword+"%' ";
		}
		sql+=" order by id desc";
		logger.debug("sql: " + sql);
		ModelGenerator<Channel> dbcpmg = new ModelGenerator<Channel>(Channel.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public List<ChannelTerraceInfo> getBychannelId(Integer channelId) {
		String sql = "select  b.id as terraceId ,b.operatorsName, a.remark as glremark ,a.id, a.relevanceid,a.dividetype,a.contractdivide,a.channelName, a.channelpass from "
			+"(select * from t_terrace ) b"
			+" left join t_channelterrace a"
			+" on a.terraceId=b.id and a.channelId="+channelId+"" ;
		logger.debug("sql: " + sql);
		ModelGenerator<ChannelTerraceInfo> dbcpmg = new ModelGenerator<ChannelTerraceInfo>(ChannelTerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	//更新渠道平台信息
	@Override
	public boolean updateChannelTerrace(Channel channel,
			List<ChannelTerraceInfo> channelTerraceInfos) {
			if(channelTerraceInfos==null){
				channelTerraceInfos =new ArrayList<ChannelTerraceInfo>();
			}
			
			//更新渠道信息
			this.updateChannel(channel);
			
			String sql = "delete from t_channelterrace where channelid=" + channel.getId();
			ConnectionManager.getInstance().execute(base, sql);
			
			for(ChannelTerraceInfo channelTerraceInfo : channelTerraceInfos){
				if (channelTerraceInfo.getChannelId() <= 0)
					channelTerraceInfo.setChannelId(channel.getId());
				this.insertChannelTerraceInfo(channelTerraceInfo);
			}
			
		return true;
	}
	/*public boolean updateChannelTerraceInfo(ChannelTerraceInfo channelTerraceInfo){
		String sql = 
				"update t_channelterrace set channelId='" + String.valueOf(channelTerraceInfo.getChannelId()) + "',"+
						"terraceId='" + String.valueOf(channelTerraceInfo.getTerraceId()) + "'," +
						"relevanceid='" + String.valueOf(channelTerraceInfo.getRelevanceid()) + "'," +
						"contractdivide='" + String.valueOf(channelTerraceInfo.getContractdivide()) + "'," +
						"dividetype='" + channelTerraceInfo.getDividetype() + "'," + 
						"channelName='" + channelTerraceInfo.getChannelName() + "'," +
						"channelpass='" + channelTerraceInfo.getChannelpass() + "'," +
						"remark='" + channelTerraceInfo.getGlremark()+"' " +
						" where id=" + String.valueOf(channelTerraceInfo.getId()) + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}*/
	
	/**
	 * 添加渠道和渠道平台关联信息
	 */
	public boolean insertChannelTerrace(Channel channel,
			List<ChannelTerraceInfo> channelTerraceInfos) {
		if(channelTerraceInfos==null){
			channelTerraceInfos =new ArrayList<ChannelTerraceInfo>();
		}
		//添加渠道信息
		this.insertChannel(channel);
		//添加渠道平台关联信息
		for(ChannelTerraceInfo ct: channelTerraceInfos){
			ct.setChannelId(channel.getId());
			if(ct.getRelevanceid().equals("-1")){
				ct.setRelevanceid(channel.getId()+"");
			}
			this.insertChannelTerraceInfo(ct);
		}
		
		return true;
	}
	
	public boolean insertChannelTerraceInfo(ChannelTerraceInfo channelTerraceInfo){		
		channelTerraceInfo.setId(getTableId("t_channelterrace"));
		String sql = 
			"insert into t_channelterrace(id,channelId,terraceId,relevanceid,contractdivide,dividetype,channelName,channelpass,remark) " +
			"values(" + String.valueOf(channelTerraceInfo.getId()) + ","+String .valueOf(channelTerraceInfo.getChannelId()) +"," +String.valueOf(channelTerraceInfo.getTerraceId())+ ",'"  +
			channelTerraceInfo.getRelevanceid() + "','"+ String.valueOf(channelTerraceInfo.getContractdivide()) + "','" + channelTerraceInfo.getDividetype() + "','" + 
			channelTerraceInfo.getChannelName()+ "','"+channelTerraceInfo.getChannelpass()+"','"+channelTerraceInfo.getGlremark()+"')";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<ChannelTerraceInfo> getChannelTerraceInfos() {
		String sql = "select  b.id as terraceId ,b.operatorsName, a.remark as glremark ,a.id,a.channelId, a.relevanceid,a.dividetype,a.contractdivide,a.channelName, a.channelpass from "
				+" t_channelterrace a, t_terrace b "
				+" where a.terraceId=b.id" ;
			logger.debug("sql: " + sql);
		ModelGenerator<ChannelTerraceInfo> dbcpmg = new ModelGenerator<ChannelTerraceInfo>(ChannelTerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public List<Channel> getChannelsByType(String type) {
		String sql = "select * from t_channel where cooperationtype='"+type+"' order by id desc";
		logger.debug("sql: " + sql);
		ModelGenerator<Channel> dbcpmg = new ModelGenerator<Channel>(Channel.class);
		return dbcpmg.generateList(base, sql);
	}

}
